<template>
  <div>
    <h2>customRef</h2>
    <input type="text" v-model="keyWord" />
    <h2>{{ keyWord }}</h2>
  </div>
</template>

<script lang="ts">
import { defineComponent, customRef } from 'vue'
function UseDobunceRef<T>(value: T, delay: number) {
  let timer: number
  return customRef((track, trigger) => {
    return {
      get() {
        track()
        return value
      },
      set(newValue: T) {
        clearTimeout(timer)
        timer = setTimeout(() => {
          value = newValue
          trigger()
        }, delay)
      }
    }
  })
}
// 自定义防抖函数
export default defineComponent({
  name: 'App',
  setup() {
    const keyWord = UseDobunceRef('哈哈', 2000)

    return {
      keyWord
    }
  }
})
</script>

<style scoped>
</style>